#include "CTS_Layer.h"

#define DELAYVIEW 50 // Timer delay timeout count - 50*0.1msec = 5 msec
#define DELAY_BUTTON_SAMPLING (300/(2*5)) // Delay before the next button sampling

#ifdef ELEMENT_CHARACTERIZATION_MODE
// Delta Counts returned from the API function for the sensor during characterization
unsigned int dCnt;
#endif


// Sleep Function
// Configures Timer A to run off ACLK, count in UP mode, places the CPU in LPM3
// and enables the interrupt vector to jump to ISR upon timeout
void sleep ( unsigned int time){
  TA0CCR0 = time;
  TA0CTL = TASSEL_1+MC_1+TACLR;
  TA0CCTL0 &= ~CCIFG;
  TA0CCTL0 |= CCIE;
  __bis_SR_register (LPM3_bits+GIE);
  __no_operation ();
}


// Main Function
void main(void ){

  // Initialize System Clocks
  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
  BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1, 8, 12 or 16MHz
  DCOCTL = CALDCO_1MHZ;
  BCSCTL2 |= DIVS_2; // divide SMCLK by 4 for 250khz
  BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO



  P1DIR = (BIT3 + BIT4 + BIT5 + BIT6 + BIT7);
  P1OUT = 0x00;
  P1OUT |= BIT3;
  P1OUT |= BIT7;
  P1OUT |= BIT6;
  P1OUT |= BIT5;
  P1OUT |= BIT4;

  // Initialize Baseline measurement
  TI_CAPT_Init_Baseline(&one_button);

  // Update baseline measurement (Average 5 measurements)
  TI_CAPT_Update_Baseline(&one_button,5);


  unsigned int count = 0;
  int aux;
  unsigned char buttonSampling;
  unsigned int countButtonSampling = 0;

  // Main loop starts here
  while (1){

    if(buttonSampling){

      #ifdef ELEMENT_CHARACTERIZATION_MODE
      TI_CAPT_Custom(&one_button,&dCnt);
      __no_operation ();

      #endif

      #ifndef ELEMENT_CHARACTERIZATION_MODE
      if (TI_CAPT_Button(&one_button)){
        // Desables the sampling for a certain period of time
        buttonSampling = 0x00;
        count++;
        if (count == 256) count = 0;
      }
    } else {
      // Count the button sampling delay
      countButtonSampling++;
      // If the delay is over, restarts the sampling
      if(countButtonSampling == DELAY_BUTTON_SAMPLING){
        buttonSampling = 0x01;
        countButtonSampling = 0;
      }

    }
    aux = count;
    P1OUT |= BIT3;
    P1OUT |= BIT7;
    P1OUT |= BIT6;
    P1OUT |= BIT5;
    P1OUT |= BIT4;
    if (aux%2 == 0){P1OUT |= BIT7;}else{P1OUT &=~ BIT7;}
    aux = aux/2;
    if (aux%2 == 0){P1OUT |= BIT6;}else{P1OUT &=~ BIT6;}
    aux = aux/2;
    if (aux%2 == 0){P1OUT |= BIT5;}else{P1OUT &=~ BIT5;}
    aux = aux/2;
    if (aux%2 == 0){P1OUT |= BIT4;}else{P1OUT &=~ BIT4;}
    sleep (DELAYVIEW);

    P1OUT &=~ BIT3;
    P1OUT &=~ BIT7;
    P1OUT &=~ BIT6;
    P1OUT &=~ BIT5;
    P1OUT &=~ BIT4;
    aux = aux/2;
    if (aux%2 == 0){P1OUT &=~ BIT7;}else{P1OUT |= BIT7;}
    aux = aux/2;
    if (aux%2 == 0){P1OUT &=~ BIT6;}else{P1OUT |= BIT6;}
    aux = aux/2;
    if (aux%2 == 0){P1OUT &=~ BIT5;}else{P1OUT |= BIT5;}
    aux = aux/2;
    if (aux%2 == 0){P1OUT &=~ BIT4;}else{P1OUT |= BIT4;}

    // Put the MSP430 into LPM3 for a certain DELAY period
    sleep (DELAYVIEW);

    #endif

  }
} // End Main

/* *****************************************************************************/
// Timer0_A0 Interrupt Service Routine : Disables the timer and exists LPM3
/* *****************************************************************************/
#pragma vector=TIMER0_A0_VECTOR
__interrupt void ISR_Timer0_A0(void){
  TA0CTL &= ~(MC_1);
  TA0CCTL0 &= ~(CCIE);
  __bic_SR_register_on_exit (LPM3_bits+GIE);
}

